<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="开通时间">
        <el-date-picker
          v-model="createTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          :default-time="['00:00:00', '23:59:59']"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构所属渠道" prop="parentId">
        <Treeselect
          ref="Treeselect"
          style="width: 250px;"
          v-model="queryParams.parentId"
          :options="channelOptions"
          :normalizer="normalizer"
          :show-count="true"
          placeholder="请选择渠道"
          noResultsText="暂无数据"
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开通机构" prop="deptId">
        <SelectCompany
          :defaultValue.sync="queryParams.deptId"
          :companyType="0"
          valueKey="deptId"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开通状态" prop="deleted">
        <el-select v-model="queryParams.deleted" placeholder="请选择开通状态" clearable @change="handleQuery">
          <el-option label="已开通" :value="0"></el-option>
          <el-option label="未开通" :value="1"></el-option>
        </el-select>

      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="开通应用" align="center" prop="applicationName" />
      <el-table-column label="开通时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开通机构" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.saasType == 0">（平台）</span>
          <span v-if="scope.row.saasType == 1">（渠道）</span>
          <span v-if="scope.row.saasType == 2">（单位）</span>
          <span>{{ scope.row.deptName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开通人" align="center" prop="createBy" />

      <el-table-column label="开通状态" align="center" prop="deleted">
        <template slot-scope="{ row }">
          <el-tag :type="row.deleted === 0 ? 'success' : 'danger'">
            {{ row.deleted === 0 ? "已开通" : "未开通" }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { openingDetailsList } from "@/api/system/apply.js";
import SelectCompany from "@/components/SelectCompany";
import { getPickerShortcuts } from '@/utils'
import { treeselect } from "@/api/channel/channel";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "OpeningDetails",
  dicts: [],
  components: {
    SelectCompany,
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 查询参数
      pickerOptions: {
        shortcuts: getPickerShortcuts()
      },
      // 表格数据
      dataList: [],
      // 查询参数
      createTime: [],

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicationId: undefined,

        parentId: undefined,
        deptId: undefined,
        createTimegt: "", // 开始时间
        createTimelt: "", // 结束时间
        deleted: undefined
      },
      // 渠道树选项
      channelOptions: [],
      normalizer(node) {
        return {
          id: node.deptId,
          label: node.label,
          children: node.children,
        }
      }
    };
  },
  created() {
    this.queryParams.applicationId = this.$route.query.applicationId;
    this.getList();
    this.getTreeselect();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      this.queryParams.createTimegt = this.createTime && this.createTime.length ? this.createTime[0] : "";
      this.queryParams.createTimelt = this.createTime && this.createTime.length ? this.createTime[1] : "";
      openingDetailsList(this.queryParams).then((res) => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 查询渠道下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.channelOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>

<style lang="scss" scoped>
/* 可以根据需要添加样式 */
</style>
